# encoding=utf8
import sys
from FT_0082 import FT_0082
reload(sys)
sys.setdefaultencoding('utf8')
myclass=FT_0082()
class FT_058f:
    result=[]
    analysisDecription = ['自定义消息体长度：', 'VIN:', '密钥长度:', '密钥：','配置版本信息：','端口序号：','ECM VIN：',
                          '附加信息项个数：','附加信息项列表']
    analysis058fLength=[2,18,1,0,10,1,18,1,0]
    total=0

    attribute_list = ['属性ID：', '属性长度：', '属性值：']
    atb_list_len = [1, 1, 0]
    terminal_attributes=myclass.terminal_attributes
    def LogicAisle(self, protocolStr, header):
        count=0
        for i in range(len(self.analysisDecription)-1):
            s=protocolStr[count:count+self.analysis058fLength[i]*2]
            print(s)
            if i==2:
                val_len=int(s,16)
                self.analysis058fLength[i+1]=val_len
                header.append(self.analysisDecription[i]+str(val_len)+'\n')
            elif i==4:
                info=s.decode("hex").decode('gb2312')
                header.append(self.analysisDecription[i]+info+'\n')
            elif i==5:
                atb_val=int(s,16)
                msg = ''
                if atb_val == 1:
                    msg = '：欧曼'
                elif atb_val == 2:
                    msg = '：奥铃'
                elif atb_val == 3:
                    msg = '：伽途'
                elif atb_val == 4:
                    msg = '：时代'
                elif atb_val == 5:
                    msg = '：萨瓦纳 '
                elif atb_val == 6:
                    msg = '：拓路者'
                elif atb_val == 7:
                    msg = '：欧马可'
                elif atb_val == 8:
                    msg = '：雷萨'
                elif atb_val == 0:
                    msg = '：默认端口'
                header.append(self.analysisDecription[i]+s+msg+'\n')
            elif i==7:
                self.total=int(s,16)
                header.append(self.analysisDecription[i]+str(self.total)+'\n')
            else:
                header.append(self.analysisDecription[i]+s+'\n')
            count +=self.analysis058fLength[i]*2
        s=protocolStr[count:-4]
        print(s)
        print(self.total)
        for i in range(self.total):
            atb_id = s[:2]
            atb_len = int(s[2:4], 16)
            print(atb_len)
            atb_val = s[4:4 + atb_len * 2]
            s = s[4 + atb_len * 2:]
            self.atb_list_len[2] = atb_len
            atb_des = ''
            atb_remark = ''
            for atb in self.terminal_attributes:
                if int(atb['id']) == int(atb_id, 16):
                    atb_des = atb['des']
                    atb_remark = atb['remark']
                    break
                else:
                    pass
            header.append(self.attribute_list[0] + str(int(atb_id,16)) + '\n')
            header.append(self.attribute_list[1] + str(atb_len) + '\n')
            atb_id = int(atb_id, 16)
            if atb_id == 1 or atb_id == 2 or atb_id == 4 or atb_id == 5 or atb_id == 6 or atb_id == 7 or atb_id == 8 or atb_id == 9 or atb_id == 10 or atb_id == 11 \
                    or atb_id == 12 or atb_id == 16 or atb_id == 17 or atb_id == 19:
                atb_val = atb_val.decode("hex").decode('gb2312')
                header.append(self.attribute_list[2] + atb_des + '' + atb_val + '' + atb_remark + '\n')
            elif atb_id == 13 or atb_id == 31:
                atb_val = int(atb_val, 16)
                header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + '' + atb_remark + '\n')
            elif atb_id == 14:
                msg = ''
                atb_val = int(atb_val, 16)
                if atb_val == 1:
                    msg = '：大型汽车'
                elif atb_val == 2:
                    msg = '：使馆汽车'
                elif atb_val == 3:
                    msg = '：领馆汽车'
                elif atb_val == 4:
                    msg = '：港澳入出境车'
                elif atb_val == 5:
                    msg = '：教练汽车 '
                elif atb_val == 6:
                    msg = '：警用汽车'
                elif atb_val == 7:
                    msg = '：挂车'
                elif atb_val == 8:
                    msg = '：低速车'
                elif atb_val == 9:
                    msg = '：临时行驶车'
                elif atb_val == 10:
                    msg = '：临时入境车'
                elif atb_val == 11:
                    msg = '：小型汽车'
                header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + msg + atb_remark + '\n')
            elif atb_id == 18:
                msg = ''
                atb_val = int(atb_val, 16)
                if atb_val == 1:
                    msg = '：欧曼'
                elif atb_val == 2:
                    msg = '：奥铃'
                elif atb_val == 3:
                    msg = '：伽途'
                elif atb_val == 4:
                    msg = '：时代'
                elif atb_val == 5:
                    msg = '：萨瓦纳 '
                elif atb_val == 6:
                    msg = '：拓路者'
                elif atb_val == 7:
                    msg = '：欧马可'
                elif atb_val == 8:
                    msg = '：雷萨'
                elif atb_val == 0:
                    msg = '：默认端口'
                header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + msg + atb_remark + '\n')
            elif atb_id == 30:
                msg = ''
                if atb_val == "00":
                    msg = '：未知'
                elif atb_val == "01":
                    msg = '：非贷款车'
                elif atb_val == "02":
                    msg = '：福田自营贷款车'
                elif atb_val == "03":
                    msg = '：非自营贷款车'
                header.append(self.attribute_list[2] + atb_des + '' + atb_val + msg + atb_remark + '\n')
            else:
                header.append(self.attribute_list[2] + atb_des + '' + atb_val + ' \n')
        self.result=header
